package com.hbue.water.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hbue.water.entity.History;
import com.hbue.water.mapper.HistoryMapper;
import com.hbue.water.service.HistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yushu
 * @date 2021/12/25
 * @apiNate
 */
@Service
@Slf4j
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    @Override
    public PageInfo<History> listHistory(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<History> historyList = this.listHistory();
        return new PageInfo<>(historyList);
    }

    @Override
    public List<History> listHistory() {
        return historyMapper.listHistory();
    }

    @Override
    public int saveHistory(History history) {
        return historyMapper.saveHistory(history);
    }

    /**
     * 根据送水历史ID查询对应的送水历史
     * 用途：修改之前的数据回显
     *
     * @param hid 送水历史ID
     * @return 送水历史信息
     */
    @Override
    public History getHistoryById(Integer hid) {
        return historyMapper.getHistoryById(hid);
    }

    /**
     * 根据客户id查询是否对应的送水历史
     * @param cid
     * @return
     */
    @Override
    public Integer getCountById(Integer cid) {
        QueryWrapper<History> qw = new QueryWrapper<>();
        qw.eq("cust_id", cid);
        return historyMapper.selectCount(qw);
    }

    /**
     * 修改送水历史
     *
     * @param history 表单采集的的送水历史信息
     * @return update语句受影响行数，大于0修改成功，否则修改失败
     */
    @Override
    public int updateHistory(History history) {
        return historyMapper.updateHistory(history);
    }

    @Override
    public PageInfo<History> searchHistoryByCondition(int currentPageNum, int pageSize, String beginDate, String endDate) {
        PageHelper.startPage(currentPageNum, pageSize);
        if (StrUtil.isEmpty(endDate)){
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            endDate = dtf.format(currentDate);
        }
        List<History> historyList = historyMapper.searchHistoryByCondition(beginDate, endDate);
        return new PageInfo<>(historyList);
    }

    /**
     * 批量删除
     * 步骤：
     *  1 字符串id转换为List集合
     *  2 调用Mapper的批量删除方法
     * @param idList 送水历史编号。例如：1,2,3,5,6,7
     * @return 大于0删除成功，否则删除失败
     */
    @Override
    public int batchDeleteHistory(String idList) {
        List<Integer> ids = new ArrayList<>();
        // 使用逗号作为分割器，切割字符串。然后转换为数组
        String[] split = StrUtil.split(idList, ",");
        for (String  id : split) {
            if (StrUtil.isNotEmpty(id)) {
                ids.add(Integer.parseInt(id));
            }
        }
        if (!ids.isEmpty() && ids.size() == 0){
            return 0;
        } else {
            QueryWrapper<History> qw = new QueryWrapper<>();
            qw.in("hid", ids);
            return historyMapper.delete(qw);
        }
    }
}
